package com.junyidao.game.service;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junyidao.game.Exception.BusinessException;
import com.junyidao.game.constant.BaseConstant;
import com.junyidao.game.constant.GroupConstant;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.mapper.TChannelGamesMapper;
import com.junyidao.game.mapper.TChannelMapper;
import com.junyidao.game.mapper.TChannelUserRelaMapper;
import com.junyidao.game.mapper.TGamesMapper;
import com.junyidao.game.pojo.*;
import com.junyidao.game.vo.GameVo;
import com.junyidao.game.vo.TChannelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HP on 2016/8/13.
 */
@Service
@Transactional
public class ChannelService {

    @Autowired
    private TChannelMapper channelMapper;

    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private TGamesMapper gamesMapper;

    @Autowired
    private TChannelGamesMapper channelGamesMapper;

    @Autowired
    private TChannelUserRelaMapper channelUserRelaMapper;

    public List<TChannel> selectChannelBuUid(SysUser user) {
        List<TChannel> list = channelMapper.selectChannelBuUid(user);
        return list;
    }

    public List<TChannel> selectChannelByChannel(TChannelVo channel) {
        List<TChannel> list = channelMapper.selectChannelByChannel(channel);
        return list;
    }

    public Map<String, Object> getAll(TChannelVo channel) {
        PageHelper.startPage(channel.getPage(), channel.getRows());
        channel.setUid(LoginInfo.getUser().getUid());
        List<TChannel> list = channelMapper.selectChannelByChannel(channel);
        PageInfo page = new PageInfo(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", page.getTotal());
        return map;
    }

    public Map<String, Object> selectChannel(TChannelVo channel) {
        PageHelper.startPage(channel.getPage(), channel.getRows());
        channel.setUid(LoginInfo.getUser().getUid());
        List<TChannel> list = channelMapper.selectChannel(channel);
        PageInfo page = new PageInfo(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", page.getTotal());
        return map;
    }

    public void activate(String ids, String groupStatus) {
        List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", idList);
        map.put("groupStatus", groupStatus);
        channelMapper.activateChannel(map);
    }

    public Integer addChannel(TChannel channel) {
        TChannel channel1 = new TChannel();
        channel1.setChannelName(channel.getChannelName());
        List<TChannel> channelList = channelMapper.select(channel1);
        if (channelList.size() > 0) {
            BusinessException.throwException(1, "渠道已存在，请修改渠道名");
        }
        Integer id = sequenceService.getNextSequence("CHANNEL_ID");
        channel.setChannelId(id);
        channel.setCreateTime(new Date());
        channel.setChannelStatus(BaseConstant.STATUS_NORMAL);
        channelMapper.insert(channel);

        TChannelUserRela rela = new TChannelUserRela();
        Integer id2 = sequenceService.getNextSequence("CHANNEL_USER_ID");
        rela.setChannelUserId(id2);
        rela.setChannelId(channel.getChannelId());
        rela.setUid(LoginInfo.getUser().getUid());
        rela.setStatusCode(BaseConstant.STATUS_NORMAL);
        channelUserRelaMapper.insert(rela);

        return channel.getChannelId();

    }

    public Map<String, Object> getGame(GameVo game) {
        PageHelper.startPage(game.getPage(), game.getRows());
        game.setUid(LoginInfo.getUser().getUid());
        List<GameVo> list = gamesMapper.selectGameByGameVo(game);
        PageInfo page = new PageInfo(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", page.getTotal());
        return map;
    }

    public void addGame(String ids, Integer channelId) {
        List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
        TChannel par = new TChannel();
        par.setChannelId(channelId);
        TChannel channel = channelMapper.selectOne(par);
        if (channel == null) {
            BusinessException.throwException(1, "渠道不存在");
        }
        for (Integer gameId : idList) {
            TChannelGames rela = new TChannelGames();
            Integer id = sequenceService.getNextSequence("CHANEL_GAMES_ID");
            rela.setChannelId(channelId);
            rela.setChannelGameId(id);
            rela.setAddTime(new Date());
            rela.setStatusCode(BaseConstant.STATUS_NORMAL);
            rela.setGameId(gameId);
            channelGamesMapper.insert(rela);
        }
    }

    public void delGame(String ids, Integer channelId) {
        List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
        Map<String, Object> map = new HashMap<>();
        map.put("channelId", channelId);
        map.put("ids", idList);
        channelGamesMapper.delChannelGame(map);
    }

    public TChannel selectChannelByChannelId(Integer id) {
        TChannel channel = new TChannel();
        channel.setChannelId(id);
        List<TChannel> list = channelMapper.select(channel);
        return list.size() > 0 ? list.get(0) : null;
    }

    public void updatePartByPrimaryKey(TChannel channel) {
        channelMapper.updatePartByPrimaryKey(channel);
    }

    public void moveOutUser(String cuid) {
        List<Integer> idList = JSONArray.parseArray(cuid, Integer.class);
        TChannelUserRela rela = new TChannelUserRela();
        for (Integer id : idList) {
            rela.setChannelUserId(id);
            channelUserRelaMapper.delete(rela);
        }
    }

    public Map<String, Object> selectGameByChannelIds(GameVo game, String channelIds) {
        PageHelper.startPage(game.getPage(), game.getRows());
        game.setUid(LoginInfo.getUser().getUid());
        List<Integer> idList = JSONArray.parseArray(channelIds, Integer.class);
        if (idList != null && idList.size() > 0) {
            game.setChannelIds(idList);
        }
        List<GameVo> list = gamesMapper.selectGameByChannelIds(game);
        PageInfo page = new PageInfo(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", page.getTotal());
        return map;
    }

    public void addGame(String channelIds, String gameIds) {
        List<Integer> channelList = JSONArray.parseArray(channelIds, Integer.class);
        List<Integer> gamesList = JSONArray.parseArray(gameIds, Integer.class);
        for (Integer cid : channelList) {
            for (Integer gid : gamesList) {
                TChannelGames rela = new TChannelGames();
                Integer id = sequenceService.getNextSequence("CHANEL_GAMES_ID");
                rela.setChannelId(cid);
                rela.setChannelGameId(id);
                rela.setAddTime(new Date());
                rela.setStatusCode(BaseConstant.STATUS_NORMAL);
                rela.setGameId(gid);
                channelGamesMapper.insert(rela);
            }
        }
    }

    public void delGame(String channelIds, String gameIds) {
        List<Integer> channelList = JSONArray.parseArray(channelIds, Integer.class);
        List<Integer> gamesList = JSONArray.parseArray(gameIds, Integer.class);
        Map<String, Object> map = new HashMap<>();
        map.put("channelList", channelList);
        map.put("gamesList", gamesList);
        channelGamesMapper.delChannelGameMore(map);
    }

}
